package com.gasstion.controller;

import com.gasstion.dao.RolePermissionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/rolePer")
@Scope
public class RolepermissionController {
    @Autowired
    public RolePermissionDao rolepermissionDao;


    @RequestMapping("/grantPer")
    @ResponseBody
    public Map<String,Object> grantPermission(int rId,String pmId){
        Map<String,Object> map=new HashMap<>();
        try {
            String[] ss=pmId.split("-");
            int[] perids=new int[ss.length];// 所有选中的权限id
            for(int i=0;i<ss.length;i++){
                perids[i]=Integer.parseInt(ss[i]);
            }
            //删除角色对应的权限
            rolepermissionDao.del(rId);
            //增加用户选择的权限
            for(int perId:perids){
                rolepermissionDao.save(rId,perId);
            }
            map.put("msg","1");
        } catch (NumberFormatException e) {
            map.put("msg","2");
            e.printStackTrace();
        }
        return map;
    }
}
